package com.pan.admin.module.channel.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.channel.dao.LoanChannelRegisterFieldDao;
import com.pan.admin.module.channel.entity.LoanChannelRegisterField;
import com.pan.admin.module.channel.service.LoanChannelRegisterFieldService;
import com.pan.admin.module.loanConfig.dao.LoanRegisterFieldMapper;
import com.pan.admin.module.loanConfig.entity.LoanRegisterField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 渠道关联注册字段配置Service业务层处理
 *
 * @author ts
 * @date 2023-09-18
 */
@Service
public class LoanChannelRegisterFieldServiceImpl implements LoanChannelRegisterFieldService {
    @Autowired
    private LoanChannelRegisterFieldDao loanChannelRegisterFieldDao;
    @Autowired
    private LoanRegisterFieldMapper fieldDao;

    /**
     * 根据ID获取列表渠道关联注册字段配置
     *
     * @param id 渠道关联注册字段配置主键
     * @return 渠道关联注册字段配置
     */
    @Override
    public LoanChannelRegisterField getById(String id) {
        return loanChannelRegisterFieldDao.getById(id);
    }

    /**
     * 查询渠道关联注册字段配置列表
     *
     * @param loanChannelRegisterField 渠道关联注册字段配置
     * @return 渠道关联注册字段配置
     */
    @Override
    public List<LoanChannelRegisterField> list(LoanChannelRegisterField loanChannelRegisterField) {
        return loanChannelRegisterFieldDao.list(loanChannelRegisterField);
    }

    /**
     * 树控列表
     *
     * @param loanChannelRegisterField
     * @return
     */
    @Override
    public List treeList(LoanChannelRegisterField loanChannelRegisterField) {
        List<LoanChannelRegisterField> list = loanChannelRegisterFieldDao.list(loanChannelRegisterField);
        List list1 = new ArrayList<>();
        if (!ValidParam.isEmpty(list)) {
            for (LoanChannelRegisterField field : list) {
                list1.add(field.getLoanRegisterFieldId());
            }
        }
        return list1;
    }

    @Override
    public int addTree(List<LoanChannelRegisterField> loanChannelRegisterField) {
        int i = 0;
        if (!ValidParam.isEmpty(loanChannelRegisterField)) {

            for (LoanChannelRegisterField registerField : loanChannelRegisterField) {

                if (registerField.getLoanRegisterFieldId() == 0) {
                    List<LoanRegisterField> loanRegisterFields = fieldDao.selectLoanRegisterFieldList(null);
                    List<LoanChannelRegisterField> list = new ArrayList<>();

                    if (!ValidParam.isEmpty(loanRegisterFields)) {
                        for (LoanRegisterField registerField1 : loanRegisterFields) {
                            LoanChannelRegisterField registerField2 = new LoanChannelRegisterField();
                            registerField2.setLoanRegisterFieldId(registerField1.getId());
                            registerField2.setLoanChannelId(registerField.getLoanChannelId());

                            int row = loanChannelRegisterFieldDao.count(registerField2);
                            if (row == 0) {
                                registerField2.setCreateTime(DateUtils.getNowDate());
                                list.add(registerField2);
                            }
                        }
                    }

                    if (!ValidParam.isEmpty(list)) {
                        i = loanChannelRegisterFieldDao.batchInsert(list);
                    }

                }

                if (registerField.getLoanRegisterFieldId() != 0) {
                    int count = loanChannelRegisterFieldDao.count(registerField);
                    if (count == 0) {
                        registerField.setCreateBy(SecurityUtils.getUsername());
                        registerField.setCreateTime(DateUtils.getNowDate());
                        i = loanChannelRegisterFieldDao.insertLoanChannelRegisterField(registerField);
                    }
                }

            }
        }

        return i;
    }

    /**
     * 删除树控节点
     */
    @Override
    public int delTree(List<LoanChannelRegisterField> loanChannelRegisterField) {
        int i = 0;
        if (!ValidParam.isEmpty(loanChannelRegisterField)) {
            i = loanChannelRegisterFieldDao.batchDeleteByTree(loanChannelRegisterField);
        }
        return i;
    }

    /**
     * 新增渠道关联注册字段配置
     *
     * @param loanChannelRegisterField 渠道关联注册字段配置
     * @return 结果
     */
    @Override
    public int insertLoanChannelRegisterField(LoanChannelRegisterField loanChannelRegisterField) {
        loanChannelRegisterField.setCreateTime(DateUtils.getNowDate());
        loanChannelRegisterField.setCreateBy(SecurityUtils.getUsername());
        return loanChannelRegisterFieldDao.insertLoanChannelRegisterField(loanChannelRegisterField);
    }

    /**
     * 修改渠道关联注册字段配置
     *
     * @param loanChannelRegisterField 渠道关联注册字段配置
     * @return 结果
     */
    @Override
    public int updateLoanChannelRegisterField(LoanChannelRegisterField loanChannelRegisterField) {
        loanChannelRegisterField.setUpdateTime(DateUtils.getNowDate());
        loanChannelRegisterField.setUpdateBy(SecurityUtils.getUsername());
        return loanChannelRegisterFieldDao.updateLoanChannelRegisterField(loanChannelRegisterField);
    }

    /**
     * 批量删除渠道关联注册字段配置
     *
     * @param ids 需要删除的渠道关联注册字段配置主键
     * @return 结果
     */
    @Override
    public int batchDeleteById(Long[] ids) {
        return loanChannelRegisterFieldDao.batchDeleteById(ids);
    }


    /**
     * 删除渠道关联注册字段配置信息
     *
     * @param id 渠道关联注册字段配置主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id) {
        return loanChannelRegisterFieldDao.deleteById(id);
    }
}
